package com.taxshare.operation.controller;

import static com.taxshare.operation.constant.MinIOConstant.*;

import com.taxshare.minio.utils.MinIOUtils;
import com.taxshare.response.beans.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * MinIO测试类
 * @author jiangye
 */
@RestController
@RequestMapping("/minio")
@Api(tags = "MinIO测试")
public class MinIOController {

    @Autowired
    private MinIOUtils minIOUtils;

    @PostMapping(value = "/uploadOri")
    @ApiOperation(notes = "文件管理", value = "文件上传【使用原文件名】")
    public Response testUpload(@RequestParam("files") MultipartFile files){
        String fileName=files.getOriginalFilename();
        minIOUtils.upload(BUCKET_NAME,GROUP_TEST ,files,false);
        return Response.success();
    }

    @PostMapping(value = "/uploaNew")
    @ApiOperation(notes = "文件管理", value = "文件上传【生成新文件名】")
    public Response testUploadNew(@RequestParam("files") MultipartFile files){
        String fileName=files.getOriginalFilename();
        minIOUtils.upload(BUCKET_NAME,GROUP_TEST,files,true);
        return Response.success();
    }

    @PostMapping(value = "/delete")
    @ApiOperation(notes = "文件管理", value = "删除文件")
    public Response delete(){
        boolean bo=minIOUtils.removeObject(BUCKET_NAME,GROUP_TEST,"MinIO测试文件.xls");
        return Response.success(bo);
    }

    @GetMapping(value = "/down")
    @ApiOperation(notes = "文件管理", value = "下载文件")
    public void down(HttpServletResponse response){
        minIOUtils.download(response,BUCKET_NAME,GROUP_TEST,"MinIO测试文件.xls");
    }
}
